package com.dofast.bt.mvp.model;

import com.dofast.bt.base.BaseApplication;
import com.dofast.bt.bean.AttentionBean;
import com.dofast.bt.bean.EnjoyBean;
import com.dofast.bt.bean.GoodsBean;
import com.dofast.bt.bean.MyCar;
import com.dofast.bt.bean.ShopBean;
import com.dofast.bt.bean.ShoppingCartBean;

import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;

import java.util.List;

/**
 * Created by Administrator on 2016/12/26 0026.
 */

public class MainModel implements IMainModel{
    @Override
    public List<GoodsBean> getGoodsByFlag( int flagId) throws DbException {
        return BaseApplication.getDbManager().selector(GoodsBean.class).where("flagId","=",flagId).findAll();
    }

    @Override
    public List<ShopBean> getShops() throws DbException {
        return BaseApplication.getDbManager().findAll(ShopBean.class);
    }

    @Override
    public List<AttentionBean> getCollectStores() throws DbException {

        return BaseApplication.getDbManager().findAll(AttentionBean.class);
    }

    @Override
    public boolean attentionShop(int shopId) throws DbException {
        if (!isAttention(shopId)){
            AttentionBean ab = new AttentionBean();
            ab.setShopId(shopId);
            BaseApplication.getDbManager().saveBindingId(ab);
            return true;
        }
        return false;
    }

    @Override
    public boolean cancelAttentionShop(int shopId) throws DbException {
        if (isAttention(shopId)){
            WhereBuilder b = WhereBuilder.b();
            b.and("shopId","=",shopId);
            BaseApplication.getDbManager().delete(AttentionBean.class,b);
            return true;
        }
        return false;
    }

    @Override
    public ShopBean getShopById(int shopId) throws DbException {
        return BaseApplication.getDbManager().findById(ShopBean.class,shopId);
    }

    @Override
    public boolean isAttention(int shopId) throws DbException {
        if (BaseApplication.getDbManager().selector(AttentionBean.class).where("shopId","=",shopId).count() > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean isEnjoyed(int goodsId) throws DbException {
        if (BaseApplication.getDbManager().selector(EnjoyBean.class).where("goodsId","=",goodsId).count() > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean isAddCar(int goodsId) throws DbException {
        if (BaseApplication.getDbManager().selector(ShoppingCartBean.class).where("goodsId","=",goodsId).count() > 0){
            return true;
        }
        return false;
    }


    @Override
    public GoodsBean getGoodsById(int goodsId) throws DbException {
        return BaseApplication.getDbManager().findById(GoodsBean.class,goodsId);
    }

    @Override
    public List<EnjoyBean> getEnjoy() throws DbException {
        return BaseApplication.getDbManager().selector(EnjoyBean.class).orderBy("enjoyId",true).findAll();
    }

    @Override
    public boolean enjoyGoods(int goodsId) throws DbException {
        if (!isEnjoyed(goodsId)){
            EnjoyBean eb = new EnjoyBean();
            eb.setGoodsId(goodsId);
            BaseApplication.getDbManager().saveBindingId(eb);
            return true;
        }
        return false;
    }

    @Override
    public boolean cancelEnjoyGoods(int goodsId) throws DbException {
        if (isEnjoyed(goodsId)){
            WhereBuilder b = WhereBuilder.b();
            b.and("goodsId","=",goodsId);
            BaseApplication.getDbManager().delete(EnjoyBean.class,b);
            return true;
        }
        return false;
    }

    @Override
    public List<ShoppingCartBean> getShoppingCar() throws DbException {
        return BaseApplication.getDbManager().findAll(ShoppingCartBean.class);
    }

    @Override
    public boolean addCar(int goodsId) throws DbException {
        if (!isAddCar(goodsId)){
            ShoppingCartBean eb = new ShoppingCartBean();
            eb.setGoodsId(goodsId);
            eb.setNum(1);
            BaseApplication.getDbManager().saveBindingId(eb);
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteCar(int goodsId) throws DbException {
        if (isAddCar(goodsId)){
            WhereBuilder b = WhereBuilder.b();
            b.and("goodsId","=",goodsId);
            BaseApplication.getDbManager().delete(ShoppingCartBean.class,b);
            return true;
        }
        return false;
    }

    @Override
    public MyCar getMyCar() throws DbException {
        return null;
    }
}
